<?php
/* Utility Database functions */
class Utilities {
  public static function selectQuery($query) {
    $retVal = null;
    global $message;
    global $successMessage;

    try {
      if (empty($query)) {
        $GLOBALS['message'] = "Please provide a select query to be executed.";
        throw new Exception($GLOBALS['message']);
      } else {

        try {
          if (!$link = mysql_connect($GLOBALS['databaseServer'], $GLOBALS['userName'], $GLOBALS['Password'])) {
            $GLOBALS['message'] = 'An Error Occurred: Could not connect to the database. Please try again. ' . mysql_error();
            throw new Exception($GLOBALS['message']);
          }


          if (!mysql_select_db($GLOBALS['database'], $link)) {
            $GLOBALS['message'] = 'An Error Occurred: Could not connect to the database. Please try again. ' . mysql_error();
            throw new Exception($GLOBALS['message']);
          }

          $result = mysql_query($query, $link);


          if (!$result) {
            $GLOBALS['message'] = "DB Error, could not query the database. " . mysql_error();
            throw new Exception($GLOBALS['message']);
          }

          $num_rows = mysql_num_rows($result);

          if ($num_rows > 0) {
            $retVal = array();

            while ($row = mysql_fetch_assoc($result)) {
              array_push($retVal, $row);
            }

            mysql_free_result($result);
          } else {
            mysql_free_result($result);
            return null;
          }
        }//end oi try
        catch (Exception $e) {
          return null;
        }

        return $retVal;
      }
    }//end of try
    catch (Exception $ex) {
      return null;
    }
  }

  /* This function executes an insert/update/delete query */

 public static function executeQuery($query) {
    $retVal = null;

    try {
      if (empty($query)) {
        $GLOBALS['message'] = "Please provide a select query to be executed.";
        throw new Exception($GLOBALS['message']);
      } else {

        try {
          if (!$link = mysql_connect($GLOBALS['databaseServer'], $GLOBALS['userName'], $GLOBALS['Password'])) {
            $GLOBALS['message'] = 'An Error Occurred: Could not connect to the database. Please try again. ' . mysql_error();
            throw new Exception($GLOBALS['message']);
          }

          if (!mysql_select_db($GLOBALS['database'], $link)) {
            $GLOBALS['message'] = 'An Error Occurred: Could not connect to the database. Please try again. ' . mysql_error();
            throw new Exception($GLOBALS['message']);
          }

          $result = mysql_query($query, $link);

          if (!$result) {
            $GLOBALS['message'] = "DB Error, could not query the database. " . mysql_error();
            throw new Exception($GLOBALS['message']);
          }

          $num_rows = mysql_affected_rows();

          return $num_rows;
        }//end of try
        catch (Exception $e) {
          return null;
        }

        return $retVal;
      }
    }//end of try
    catch (Exception $ex) {
      return null;
    }
  }
}
?>
